﻿using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace ServerUtils
{
    public abstract class WCFServiceWithInvoker : WCFService
    {
        private readonly OperationInvoker operationInvoker;

        public WCFServiceWithInvoker(string serviceId)
        {
            this.operationInvoker = new OperationInvoker(serviceId);
        }

        public event EventHandler<LogEventArgs> Log
        {
            add { this.operationInvoker.Log += value; }
            remove { this.operationInvoker.Log -= value; }
        }

        public bool LogOperations
        {
            get { return this.operationInvoker.LogOperation; }
            set { this.operationInvoker.LogOperation = value; }
        }

        protected TResult Invoke<TResult>(IOperation<TResult> operation) where TResult : OperationResult, new()
        {
            return this.operationInvoker.Invoke(operation);
        }

        protected TResult Invoke<TResult>(Func<TResult> func, object request, [CallerMemberName]string operationName = "") where TResult : OperationResult, new()
        {
            return this.operationInvoker.Invoke(func, request, operationName);
        }

        protected Task<TResult> InvokeAsync<TResult>(IAsyncOperation<TResult> operation) where TResult : OperationResult, new()
        {
            return this.operationInvoker.InvokeAsync<TResult>(operation);
        }
    }
}
